package com.fh.modules.ems.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.copy.BeanUtils;
import com.fh.common.copy.CopyType;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.ems.dto.EmsEnterpriseDTO;
import com.fh.modules.ems.entity.EmsEnterprise;
import com.fh.modules.ems.service.IEmsEnterpriseService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Title: Controller
 * @Description: 商户管理
 * @author： qiujunda
 * @date：   2019-06-02
 * @version： V1.0
 */
@RestController
@Api("商户管理")
public class EmsEnterpriseController {
	@Autowired
	private IEmsEnterpriseService emsEnterpriseService;
	
	/**
	  * 分页列表查询
	 * @param emsEnterprise
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value = "/ems/emsEnterprise/list")
	@ApiOperation("分页列表查询")
	@AutoLog(value="商户分页查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryPageList(EmsEnterprise emsEnterprise,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
									  ) {
		QueryWrapper<EmsEnterprise> queryWrapper = new QueryWrapper<EmsEnterprise>();
		emsEnterprise.setDelFlag(CommonConstant.DEL_FLAG_0);
		QueryWrapperUtil.initQueryWrapper(queryWrapper, emsEnterprise);
		Page<EmsEnterprise> page = new Page<EmsEnterprise>(pageNo, pageSize);
		IPage<EmsEnterprise> pageList = emsEnterpriseService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	  *   添加
	 * @return
	 */
	@PostMapping(value = "/ems/emsEnterprise/add")
	@ApiOperation("添加")
	@AutoLog("添加商户")
	public Result add(@RequestBody @Validated EmsEnterpriseDTO dto) throws IllegalArgumentException, IllegalAccessException {
		EmsEnterprise emsEnterprise=new EmsEnterprise();
		BeanUtils.copy(dto, emsEnterprise, CopyType.ADD);
		emsEnterprise.setDelFlag(CommonConstant.DEL_FLAG_0);
		emsEnterprise.setAccountCurrent(0);
		emsEnterprise.setShopCurrent(0);
		boolean ok=emsEnterpriseService.addEnterprise(emsEnterprise);
		return ok?Result.ok(emsEnterprise):Result.error("添加失败");
	}
	
	/**
	  *  编辑
	 * @return
	 */
	@PutMapping(value = "/ems/emsEnterprise/edit")
	@ApiOperation("修改")
	@AutoLog("修改商户")
	public Result edit(@RequestBody @Validated EmsEnterpriseDTO dto) throws IllegalArgumentException, IllegalAccessException {
		EmsEnterprise emsEnterprise=new EmsEnterprise();
		BeanUtils.copy(dto, emsEnterprise, CopyType.UPDATE);
		boolean ok = emsEnterpriseService.updateById(emsEnterprise);
		return ok?Result.ok(emsEnterprise):Result.error("修改失败");
	}
	
	/**
	  *   通过id删除商家
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/ems/emsEnterprise/delete")
	@ApiOperation("通过id删除")
	@AutoLog("删除商户")
	public Result delete(@RequestParam(name="id",required=true) String id) {
		emsEnterpriseService.deleteEntityByDelFlag(id);
		return Result.ok("删除成功");
	}
	
	/**
	  *  批量删除商家
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/ems/emsEnterprise/deleteBatch")
	@ApiOperation("批量删除")
	@AutoLog("批量删除商户")
	public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		emsEnterpriseService.deleteBatchEntityByDelFlag(ids);
		return Result.ok("批量删除成功");
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/ems/emsEnterprise/queryById")
	@ApiOperation("通过id查询")
	@AutoLog(value="商户通过id查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryById(@RequestParam(name="id",required=true) String id) {
		EmsEnterprise emsEnterprise = emsEnterpriseService.getById(id);
		return Result.ok(emsEnterprise);
	}

}
